package com.amor.activity;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amor.R;
import com.amor.utils.ScreenUtil;
import com.amor.utils.TimeUtil;


public class BaseActivity extends AppCompatActivity {

    private LinearLayout root_layout;
    private TextView tv_left_back;
    private TextView tv_title;
    private View toolbar_layout;
    private final int TIME_SIGN_CHANGE = 426;
    private Handler timeHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case TIME_SIGN_CHANGE:
                    setTimeTittle();
                    timeHandler.removeMessages(TIME_SIGN_CHANGE);
                    timeHandler.sendEmptyMessageDelayed(TIME_SIGN_CHANGE, 1000);
                    break;
            }
        }
    };
    private TextView tv_right;
    private View back_view;
    private ProgressDialog mDialog;
    private String tittleName;

    /**
     * 主标题设置时间
     */
    private void setTimeTittle() {
        setTitle(TimeUtil.getMDHMStrDate(System.currentTimeMillis()));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        ScreenUtil.setChenjin(BaseActivity.this);
        super.setContentView(R.layout.layout_root);
        initToorBar();
        tittleName = getIntent().getStringExtra("tittle");
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        setContentView(View.inflate(this, layoutResID, null));
    }

    @Override
    public void setContentView(View view) {
        root_layout = (LinearLayout) findViewById(R.id.root_layout);
        if (root_layout != null) {
            root_layout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            initToorBar();
        }

    }

    /**
     * 设置标题名
     */
    public void setCenterTopView() {
        if (!TextUtils.isEmpty(tittleName)) {
            try {
                ((TextView) getViewById(R.id.act_top).findViewById(R.id.tv_function)).setText(tittleName);
            } catch (Exception e) {
                return;
            }
        }
    }

    /**
     * 查找组件
     *
     * @param viewId：View的ID
     * @return View
     */
    @SuppressWarnings("unchecked")
    protected <view extends View> view getViewById(int viewId) {
        return (view) findViewById(viewId);
    }

    /**
     * 初始化 toolbar 内容布局
     */
    private void initToorBar() {
        back_view = getViewById(R.id.back_view);
        tv_right = getViewById(R.id.tv_right);
        tv_left_back = getViewById(R.id.tv_back);
        tv_title = getViewById(R.id.tv_title);
        toolbar_layout = getViewById(R.id.ll_toorbar);
        hideToolbar();
        //timeHandler.sendEmptyMessageAtTime(TIME_SIGN_CHANGE, 1000);
        /*String str = Util.getOnlineUsername();
        if (!TextUtils.isEmpty(str)) {
            setBackText(str);
        }
        String compname = Util.getCompanyName();
        if (!TextUtils.isEmpty(compname)) {
            setRightText(compname);
        }*/
    }

    /**
     * 设置toobar高度
     */
    private void setToobarHeight() {
        if (toolbar_layout != null) {
            LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) toolbar_layout.getLayoutParams();
            int statusHeight = ScreenUtil.getStatusHeight(this);
            int oldheight = (int) getResources().getDimension(R.dimen.toorbar_height);
            int toorbarheitht = statusHeight + oldheight;
            Log.i("login", "sta:" + statusHeight + "|h:" + oldheight);
            linearParams.height = toorbarheitht;
            toolbar_layout.setLayoutParams(linearParams);
            toolbar_layout.setPadding(0, statusHeight, 0, 0);
        }
    }

    /**
     * 设置返回按钮功能
     */
    protected void setBack() {

    }

    protected void setRightText(String textValue) {
        if (TextUtils.isEmpty(textValue))
            textValue = "";
        if (tv_right != null) {
            tv_right.setVisibility(View.VISIBLE);
            tv_right.setText(textValue);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        setToobarHeight();
    }

    /**
     * 设置当前 Activity 标题
     *
     * @param title
     */
    protected void setTitle(String title) {
        if (tv_title != null) {
            tv_title.setVisibility(View.VISIBLE);
            tv_title.setText(title);
        }
    }

    public void hideTittLeft() {
        if (tv_left_back != null) {
            tv_left_back.setVisibility(View.GONE);
        }
    }

    /**
     * 隐藏头部标题栏
     */
    protected void hideToolbar() {
        if (toolbar_layout != null) {
            toolbar_layout.setVisibility(View.GONE);
        }
    }

    /**
     * 设置返回按钮响应事件以及显示文字
     *
     * @param backValue
     */
    private void setBackText(String backValue) {
        if (!TextUtils.isEmpty(backValue)) {
            if (tv_left_back != null && back_view != null) {
                tv_left_back.setText(backValue);
                back_view.setVisibility(View.VISIBLE);
                back_view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setBack();
                    }
                });
            }
        } else {
            hideBack();
        }
    }


    /**
     * 隐藏返回按钮
     */
    public void hideBack() {
        back_view.setVisibility(View.GONE);
    }

    /**
     * 关闭滚动窗 loulan1939
     */
    public void closeProgress() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    /**
     * 显示滚动窗
     */

    public void showProgress(String message) {
        if (mDialog == null) {
            mDialog = new ProgressDialog(this);
            mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            // 设置ProgressDialog 提示信息
            mDialog.setMessage(message);
            mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    if (mDialog != null) {
                        mDialog = null;
                    }
                }
            });
            // 设置ProgressDialog 的进度条是否不明确
            mDialog.setIndeterminate(false);

            // 设置ProgressDialog 是否可以按退回按键取消
            mDialog.setCancelable(false);

            mDialog.show();
        }
    }

    public void upDataProgress(long progress, String message) {
        if (mDialog != null) {
            mDialog.setMessage(message + progress + "%");
        }
    }

    /*public void registReceiver() {
        //注册系统广播  接受扫描到的数据
        IntentFilter iFilter = new IntentFilter();
        iFilter.addAction(RECE_DATA_ACTION);
        iFilter.addAction(BluetoothDevice.ACTION_FOUND);
        iFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        iFilter.addAction(Constant.DATA_BACK_ACTION);
        registerReceiver(receiver, iFilter);
    }

    public BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(android.content.Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(RECE_DATA_ACTION)) {
                String data = intent.getStringExtra("se4500");
                scanCallBack(data);
            } else if (action.equals(DATA_BACK_ACTION)) {
                byte[] value = intent.getByteArrayExtra("data");
                blueCallBack(value);
            }
            if (action.equals(BluetoothDevice.ACTION_FOUND) ||
                    action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED) ||
                    action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                blueListenerCallBack(intent);
            }
        }

    };*/

    protected void blueListenerCallBack(Intent intent) {

    }

    protected void scanCallBack(String data) {

    }

    protected void blueCallBack(byte[] data) {

    }

    private void unRegisterReceiver() {
        /*try {
            if (receiver != null)
                unregisterReceiver(receiver);
        } catch (Exception e) {
            return;
        }*/
    }

    @Override
    protected void onDestroy() {
        unRegisterReceiver();
        super.onDestroy();
    }
}
